import { fileURLToPath, URL } from 'node:url'

import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
// 自动导入vue中hook reactive ref等
import AutoImport from "unplugin-auto-import/vite"
//自动导入ui-组件 比如说ant-design-vue  element-plus等
import Components from 'unplugin-vue-components/vite';
//ant-design-vue
import {AntDesignVueResolver} from "unplugin-vue-components/resolvers"


// https://vitejs.dev/config/
export default defineConfig(({ command, mode})=>{
  const envConfig=loadEnv(mode,process.cwd(),'')
  console.log(envConfig)
  return ({
    plugins: [
      vue(),
      vueJsx(),
      vueDevTools(),
      AutoImport({
        //安装两行后你会发现在组件中不用再导入ref，reactive等
        imports: ['vue', 'vue-router'],
        dts: "src/auto-import.d.ts",
        //ant-design-vue
        resolvers: [AntDesignVueResolver()]
      }),
      Components({
        //ant-design-vue   importStyle = false 样式就没了，必须将样式设置为false
        resolvers: [AntDesignVueResolver({importStyle: false, resolveIcons: true})],
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
        '~': fileURLToPath(new URL('./', import.meta.url))
      }
    },
    define: {},
    envPrefix:'VITE_',//(前缀自行定义)
    server: {
      port: (Number)(envConfig.PORT),
      proxy: {
        '/api': {
          target: envConfig.BASEURL,
          // 开发模式，默认的127.0.0.1,开启后代理服务会把origin修改为目标地址
          changeOrigin: true,
          // secure: true, // 是否https接口
          // ws: true, // 是否代理websockets

          // 路径重写，**** 如果你的后端有统一前缀(如:/api)，就不开启；没有就开启
          //简单来说，就是是否改路径 加某些东西
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
  })
})

